﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
	/// <summary>
	/// 数据访问类:PayDetail
	/// </summary>
	public class PayDetail
	{
		public PayDetail()
		{}

    #region getlist
        public static DataTable GetDetail()
        {
            return DBHelper.GetDataSet("select 编号, 明细, 金额, 备注 from PayDetail");
        }
        public static DataTable GetDetailList()
        {
            return DBHelper.GetDataSet("select 序号, 明细, 金额, 备注 from PayDetail");
        }
        public static DataTable GetDetailList(string Num)
        {
            string sql = string.Format("select 明细, 金额, 备注 from PayDetail where 编号 like '%{0}%'", Num);
            return DBHelper.GetDataSet(sql);
        }
    #endregion

        /// <summary>
        ///   更新明细表数据
        /// </summary>
        //         public static int UpdateDetail()
        //         {
        //             return DAL.DBHelper.Update("select * from Test");
        //         }
        /// <summary>
        ///   更新一条数据重写
        /// </summary>
        public static int Update1(Model.PayDetail model)
        {
            string sql = "update PayDetail set 编号=model.编号, 明细=model.明细, 金额=model.金额, 备注=model.备注 where 序号= model.序号";
            return DBHelper.ExecuteCommand(sql);
        }


        #region  BasicMethod

        /// <summary>
		/// 得到最大ID
		/// </summary>
		public static int GetMaxId()
		{
		return DBHelper.GetMaxID("序号", "PayDetail"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public static bool Exists(int 序号)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from PayDetail");
			strSql.Append(" where 序号=@序号");
			SqlParameter[] parameters = {
					new SqlParameter("@序号", SqlDbType.Int,4)
			};
			parameters[0].Value = 序号;

			return DBHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public static int Add(Model.PayDetail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into PayDetail(");
			strSql.Append("编号,明细,金额,备注)");
			strSql.Append(" values (");
			strSql.Append("@编号,@明细,@金额,@备注)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@编号", SqlDbType.NVarChar),
					new SqlParameter("@明细", SqlDbType.NVarChar),
					new SqlParameter("@金额", SqlDbType.Decimal,9),
					new SqlParameter("@备注", SqlDbType.NVarChar)};
			parameters[0].Value = model.编号;
			parameters[1].Value = model.明细;
			parameters[2].Value = model.金额;
			parameters[3].Value = model.备注;

			object obj = DBHelper.ExecuteCommand(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public static int Update(Model.PayDetail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PayDetail set ");
			strSql.Append("编号=@编号,");
			strSql.Append("明细=@明细,");
			strSql.Append("金额=@金额,");
			strSql.Append("备注=@备注");
			strSql.Append(" where 序号=@序号");
			SqlParameter[] parameters = {
					new SqlParameter("@编号", SqlDbType.NVarChar),
					new SqlParameter("@明细", SqlDbType.NVarChar),
					new SqlParameter("@金额", SqlDbType.Decimal,9),
					new SqlParameter("@备注", SqlDbType.NVarChar),
					new SqlParameter("@序号", SqlDbType.Int,4)};
			parameters[0].Value = model.编号;
			parameters[1].Value = model.明细;
			parameters[2].Value = model.金额;
			parameters[3].Value = model.备注;
			parameters[4].Value = model.序号;

            return DBHelper.ExecuteSql(strSql.ToString(), parameters);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public static bool Delete(int 序号)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PayDetail ");
			strSql.Append(" where 序号=@序号");
			SqlParameter[] parameters = {
					new SqlParameter("@序号", SqlDbType.Int,4)
			};
			parameters[0].Value = 序号;

			int rows=DBHelper.ExecuteCommand(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
        public static bool DeleteList(string 序号list)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PayDetail ");
			strSql.Append(" where 序号 in ("+序号list + ")  ");
			int rows=DBHelper.ExecuteCommand(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public static Model.PayDetail GetModel(int 序号)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 序号,编号,明细,金额,备注 from PayDetail ");
			strSql.Append(" where 序号=@序号");
			SqlParameter[] parameters = {
					new SqlParameter("@序号", SqlDbType.Int,4)
			};
			parameters[0].Value = 序号;

			Model.PayDetail model=new Model.PayDetail();
			DataTable dt=DBHelper.GetDataSet(strSql.ToString(),parameters);
			if(dt.Rows.Count>0)
			{
				return DataRowToModel(dt.Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public static Model.PayDetail DataRowToModel(DataRow row)
		{
			Model.PayDetail model=new Model.PayDetail();
			if (row != null)
			{
				if(row["序号"]!=null && row["序号"].ToString()!="")
				{
					model.序号=int.Parse(row["序号"].ToString());
				}
				if(row["编号"]!=null)
				{
					model.编号=row["编号"].ToString();
				}
				if(row["明细"]!=null)
				{
					model.明细=row["明细"].ToString();
				}
				if(row["金额"]!=null && row["金额"].ToString()!="")
				{
					model.金额=decimal.Parse(row["金额"].ToString());
				}
				if(row["备注"]!=null)
				{
					model.备注=row["备注"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public static DataTable GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select 序号,编号,明细,金额,备注 ");
			strSql.Append(" FROM PayDetail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelper.GetDataSet(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" 序号,编号,明细,金额,备注 ");
			strSql.Append(" FROM PayDetail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM PayDetail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.序号 desc");
			}
			strSql.Append(")AS Row, T.*  from PayDetail T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "PayDetail";
			parameters[1].Value = "序号";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DBHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod

		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}

}
